<?php
// $Id$

/**
 * Acctuser 封装来自 acct_user 数据表的记录及领域逻辑
 */
class Acctuser extends QDB_ActiveRecord_Abstract
{

	/**
	 * 返回对象的定义
	 *
	 * @static
	 *
	 * @return array
	 */
	static function __define()
	{
		return array
		(
		// 指定该 ActiveRecord 要使用的行为插件
            'behaviors' => '',

		// 指定行为插件的配置
            'behaviors_settings' => array
		(
		# '插件名' => array('选项' => 设置),
		),

		// 用什么数据表保存对象
            'table_name' => 'acct_user',

		// 指定数据表记录字段与对象属性之间的映射关系
		// 没有在此处指定的属性，QeePHP 会自动设置将属性映射为对象的可读写属性
            'props' => array
		(
		// 主键应该是只读，确保领域对象的“不变量”
                'id' => array('readonly' => true),

		/**
		 *  可以在此添加其他属性的设置
		 */
		# 'other_prop' => array('readonly' => true),

		/**
		 * 添加对象间的关联
		 */
		# 'other' => array('has_one' => 'Class'),
				'agent'=>array('has_one'=>'acct','source_key'=>'agent_id','target_key'=>'id'),
            	'admin'=>array('has_one'=>'acct','source_key'=>'admin_id','target_key'=>'id'),
				'a_rategroup'=>array('has_one'=>'rategroup','source_key'=>'rategroupid','target_key'=>'id'),
            	'b_rategroup'=>array('has_one'=>'rategroup','source_key'=>'rategroupid_acall','target_key'=>'id'),
            	'userbind'=>array('has_many'=>'Userbindtel','source_key'=>'acctname','target_key'=>'acctname'),
				'web800'=>array('has_one'=>'web800','source_key'=>'id','target_key'=>'acctuser_id'),
				'vip1'=>array('has_one'=>'vip1','source_key'=>'id','target_key'=>'acct_id'),
				'vip2'=>array('has_one'=>'vip2','source_key'=>'id','target_key'=>'acct_id'),
				),

		/**
		 * 允许使用 mass-assignment 方式赋值的属性
		 *
		 * 如果指定了 attr_accessible，则忽略 attr_protected 的设置。
		 */
            'attr_accessible' => '',

		/**
		 * 拒绝使用 mass-assignment 方式赋值的属性
		 */
            'attr_protected' => 'id',

		/**
		 * 指定在数据库中创建对象时，哪些属性的值不允许由外部提供
		 *
		 * 这里指定的属性会在创建记录时被过滤掉，从而让数据库自行填充值。
		 */
            'create_reject' => 'createtime',

		/**
		 * 指定更新数据库中的对象时，哪些属性的值不允许由外部提供
		 */
            'update_reject' => '',

		/**
		 * 指定在数据库中创建对象时，哪些属性的值由下面指定的内容进行覆盖
		 *
		 * 如果填充值为 self::AUTOFILL_TIMESTAMP 或 self::AUTOFILL_DATETIME，
		 * 则会根据属性的类型来自动填充当前时间（整数或字符串）。
		 *
		 * 如果填充值为一个数组，则假定为 callback 方法。
		 */
            'create_autofill' => array
		(
		# 属性名 => 填充值
		# 'is_locked' => 0,
		'activedate'=>date('Y-m-d'),
		),

		/**
		 * 指定更新数据库中的对象时，哪些属性的值由下面指定的内容进行覆盖
		 *
		 * 填充值的指定规则同 create_autofill
		 */
            'update_autofill' => array
		(
		),

		/**
		 * 在保存对象时，会按照下面指定的验证规则进行验证。验证失败会抛出异常。
		 *
		 * 除了在保存时自动验证，还可以通过对象的 ::meta()->validate() 方法对数组数据进行验证。
		 *
		 * 如果需要添加一个自定义验证，应该写成
		 *
		 * 'title' => array(
		 *        array(array(__CLASS__, 'checkTitle'), '标题不能为空'),
		 * )
		 *
		 * 然后在该类中添加 checkTitle() 方法。函数原型如下：
		 *
		 * static function checkTitle($title)
		 *
		 * 该方法返回 true 表示通过验证。
		 */
            'validations' => array
		(
		/* 'acctname' => array
		 (
		 array('max_length', 30, '帐户名不能超过 30 个字符'),

		 ),

		 'password' => array
		 (
		 array('max_length', 30, '密码不能超过 30 个字符'),

		 ),

		 'acclevel' => array
		 (
		 array('is_int', '普通用户级别必须是一个整数'),

		 ),

		 'bindtel' => array
		 (
		 array('max_length', 30, '绑定电话不能超过 30 个字符'),

		 ),

		 'admin_id' => array
		 (
		 array('is_int', '超管id必须是一个整数'),

		 ),

		 'agent_id' => array
		 (
		 array('is_int', '上级代理id必须是一个整数'),

		 ),

		 'acallbillcur' => array
		 (
		 array('is_int', 'acallbillcur必须是一个整数'),

		 ),

		 'minute_len' => array
		 (
		 array('is_int', '分钟长度必须是一个整数'),

		 ),

		 'talk_timelong' => array
		 (
		 array('is_int', '通话限制必须是一个整数'),

		 ),

		 'call_cur' => array
		 (
		 array('is_int', 'call_cur必须是一个整数'),

		 ),

		 'call_count' => array
		 (
		 array('is_int', 'SIP 并发数必须是一个整数'),

		 ),

		 'cdr_update' => array
		 (
		 array('is_int', '1产生话单，0不产生话单必须是一个整数'),

		 ),

		 'monthly_call_timelong' => array
		 (
		 array('is_int', '月限制拨打时间必须是一个整数'),

		 ),

		 'updatemonthly' => array
		 (
		 array('is_int', 'updatemonthly必须是一个整数'),

		 ),

		 'updateday' => array
		 (
		 array('is_int', 'updateday必须是一个整数'),

		 ),

		 'updateyear' => array
		 (
		 array('is_int', 'updateyear必须是一个整数'),

		 ),

		 'balance' => array
		 (
		 array('is_float', '用户余额必须是一个浮点数'),

		 ),

		 'limitbalance' => array
		 (
		 array('is_float', '回铃限制金额必须是一个浮点数'),

		 ),

		 'limitbindtel' => array
		 (
		 array('is_int', '最大绑定电话数量必须是一个整数'),

		 ),

		 'calltype' => array
		 (
		 array('is_int', '拨打类型必须是一个整数'),

		 ),

		 'costbalance' => array
		 (
		 array('is_float', '已经使用余额必须是一个浮点数'),

		 ),

		 'voucherbalance' => array
		 (
		 array('is_float', '充值金额必须是一个浮点数'),

		 ),

		 'activedate' => array
		 (
		 array('is_date', '激活时间必须是一个有效的日期'),

		 ),

		 'expireddays' => array
		 (
		 array('is_int', '过期天数必须是一个整数'),

		 ),

		 'vouchercard' => array
		 (
		 array('max_length', 30, '激活的充值卡 即第一次使用的充值卡号不能超过 30 个字符'),

		 ),

		 'expireddate' => array
		 (
		 array('is_date', '过期时间必须是一个有效的日期'),

		 ),

		 'rategroupid' => array
		 (
		 array('is_int', '被叫费率组必须是一个整数'),

		 ),

		 'rategroupid_acall' => array
		 (
		 array('is_int', '回铃费率组必须是一个整数'),

		 ),

		 'gatewaygroupid' => array
		 (
		 array('is_int', '呼叫路由组必须是一个整数'),

		 ),

		 'gatewaygroupid_acall' => array
		 (
		 array('is_int', '回铃路由组必须是一个整数'),

		 ),

		 'enable' => array
		 (
		 array('is_int', '用户状态 1正常 0禁用必须是一个整数'),

		 ),

		 'callerid' => array
		 (
		 array('max_length', 20, '显示的主叫号码不能超过 20 个字符'),

		 ),

		 'bindexten' => array
		 (
		 array('max_length', 20, '直拨前缀不能超过 20 个字符'),

		 ),

		 'siptranfer' => array
		 (
		 array('is_int', '转移开关：0=不转移 1=遇忙或不在线转移 2=无条件转移必须是一个整数'),

		 ),

		 'siptranfer_callee' => array
		 (
		 array('max_length', 20, '转移的号码不能超过 20 个字符'),

		 ),

		 'callbindtelringtimelong' => array
		 (
		 array('is_int', '400等待 多少秒后呼叫下一个电话必须是一个整数'),

		 ),

		 'callbindteltype' => array
		 (
		 array('is_int', '呼入类型 -1默认 0=IVR总机流程 1=直接呼叫账号 2=一号通预约 3=400呼叫绑定号码必须是一个整数'),

		 ),

		 'bk' => array
		 (
		 array('max_length', 10, 'bk不能超过 10 个字符'),

		 ),

		 'address' => array
		 (
		 array('max_length', 255, '地址不能超过 255 个字符'),

		 ),

		 'chinesename' => array
		 (
		 array('max_length', 50, '姓名不能超过 50 个字符'),

		 ),

		 'tel' => array
		 (
		 array('max_length', 30, '电话不能超过 30 个字符'),

		 ),

		 'email' => array
		 (
		 array('max_length', 50, 'EMAIL不能超过 50 个字符'),

		 ),

		 'rpid' => array
		 (
		 array('not_empty', 'rpid不能为空'),
		 array('max_length', 64, 'rpid不能超过 64 个字符'),

		 ),

		 'ha1' => array
		 (
		 array('not_empty', 'ha1不能为空'),
		 array('max_length', 64, 'ha1不能超过 64 个字符'),

		 ),

		 'ha1b' => array
		 (
		 array('not_empty', 'ha1b不能为空'),
		 array('max_length', 64, 'ha1b不能超过 64 个字符'),

		 ),

		 'domain' => array
		 (
		 array('not_empty', 'domain不能为空'),
		 array('max_length', 64, 'domain不能超过 64 个字符'),

		 ),

		 'outcall_maxline' => array
		 (
		 array('is_int', '外乎限制并发必须是一个整数'),

		 ),
			*/

		),
		);
	}


	/* ------------------ 以下是自动生成的代码，不能修改 ------------------ */

	/**
	 * 开启一个查询，查找符合条件的对象或对象集合
	 *
	 * @static
	 *
	 * @return QDB_Select
	 */
	static function find()
	{
		$args = func_get_args();
		return QDB_ActiveRecord_Meta::instance(__CLASS__)->findByArgs($args);
	}

	/**
	 * 返回当前 ActiveRecord 类的元数据对象
	 *
	 * @static
	 *
	 * @return QDB_ActiveRecord_Meta
	 */
	static function meta()
	{
		return QDB_ActiveRecord_Meta::instance(__CLASS__);
	}


	/* ------------------ 以上是自动生成的代码，不能修改 ------------------ */
	static function use_card($acctname,$cardname,$password,$mobile = '',$unionuser = '')
	{
		
		$monthFlag = false;
		$acctname = trim($acctname);
		$newAccUser = false;
		$card = Vouchercard::find('cardname = ? and password = ? and enable = 1 and vouchered = 0 ',$cardname,$password)->getOne();
			
		if (!($card->cardname))
		{
			
			return '卡密错误或者失效';
		}else
		{
			#检测是否金额小于1元或则过了有效期
			$filterAcct = Acctuser::find('acctname = ?',trim($acctname))->getOne();
			if ($filterAcct->id())
			{
				#如果金额小于1或则过了有效期
				if ((($filterAcct->balance < 1  || strtotime($filterAcct->expireddate) < time()) || $filterAcct->enable == 0 || !Acctuser::checkParentValibalance($filterAcct->agent_id)) && ($filterAcct->agent_id != $card->agent_id && $filterAcct->changeagent != 1) )
				{
					Acctuser::meta()->deleteWhere('id = ?',$filterAcct->id);
				}
			}else{
				#不存在用户
				if ($card->cardtype == 1)
				{
					$monthFlag = true;
				}
			}
			
			
		//检测充值卡类型
		switch ($card->voucheredtype )
		{
			case 0:
				//开户
					$isone = self::find('acctname = ?',$cardname)->getCount('id');
					if($isone > 0)
					{
						return false;//存在开户卡信息的用户
					}else
					{
						$acctuser = new self();
						$acctuser->agent_id = $card->agent_id;
						$acctuser->admin_id = $card->admin_id;
						
						$acctuser->acctname = $cardname;
						$acctuser->password = $password;
						$acctuser->balance = 0;
						//$acctuser->bindexten_type = 0;
						//$acctuser->expireddays = $card->expireddays;
						$acctuser->rategroupid = $card->rategroupid;
						$acctuser->rategroupid_acall = $card->rategroupid_acall;
						$acctuser->limitbalance = $card->limitbalance;
						try {
							$acctuser->save(0,'create');
							$bind = new Userbindtel();
							$bind->admin_id = $acctuser->admin_id;
							$bind->agent_id = $acctuser->agent_id;
							$bind->acctname = $cardname;
							$bind->bindtel = $mobile;
							$bind->save(0,'create');//添加绑定号码
							//return true;
						} catch (Exception $e) {
							return false;
						}
					}
				break;
			case 1:
				//充值
			$acctuser = self::find('acctname = ? or bindtel = ?',$acctname,$acctname)->getOne();
				break;
			case 2:
				//开户充值
				if ($mobile)
				{
					$isone = self::find('acctname = ?',$mobile)->getCount('id');
					if($isone > 0)
					{
						return '已经存在此用户信息';//存在开户卡信息的用户
					}else
					{
						$acctuser = new self();
						$acctuser->agent_id = $card->agent_id;
						$acctuser->admin_id = $card->admin_id;
						
						$acctuser->acctname = $mobile;
						$acctuser->password = $password;
						$acctuser->balance = 0;
						//$acctuser->bindexten_type = 0;
						$acctuser->expireddays = $card->expireddays;
						$acctuser->rategroupid = $card->rategroupid;
						$acctuser->rategroupid_acall = $card->rategroupid_acall;
						$acctuser->limitbalance = $card->limitbalance;
						try {
							$newAccUser = true;
							$acctuser->save(0,'create');
							$bind = new Userbindtel();
							$bind->admin_id = $acctuser->admin_id;
							$bind->agent_id = $acctuser->agent_id;
							$bind->acctname = $cardname;
							$bind->bindtel = $mobile;
							$bind->save(0,'create');//添加绑定号码
							//return true;
						} catch (Exception $e) {
							return '数据库出错';
						}
					}
				}else{
					
					$acctuser = self::find('acctname = ?',$acctname)->getOne();
				
					if (!$acctuser->id())
					{
						$newAccUser = true;
						$acctuser = new self();
						$acctuser->agent_id = $card->agent_id;
						$acctuser->admin_id = $card->admin_id;
						
						$acctuser->acctname = $acctname;
						$acctuser->password = $password;
						$acctuser->balance = 0;
						//$acctuser->bindexten_type = 0;
						$acctuser->expireddays = $card->expireddays;
						$acctuser->rategroupid = $card->rategroupid;
						$acctuser->rategroupid_acall = $card->rategroupid_acall;
						$acctuser->limitbalance = $card->limitbalance;
						try {
							$acctuser->save(0,'create');
							
							/*$bind = new Userbindtel();
							$bind->admin_id = $acctuser->admin_id;
							$bind->agent_id = $acctuser->agent_id;
							$bind->acctname = $cardname;
							$bind->bindtel = $mobile;
							$bind->save(0,'create');//添加绑定号码*/
							//return true;
						} catch (Exception $e) {
							
							return '数据库出错';
						}
					}else if( ($acctuser->rategroupid != $card->rategroupid || $acctuser->rategroupid_acall != $card->rategroupid_acall) && $acctuser->changeagent == 0){
						return '费率属性必须一致';
					}
				}
				
				break;
		}
			if (!$acctuser->id())
			{
				return '数据库出错';
			}
			if( ($acctuser->agent_id != $card->agent_id || $acctuser->admin_id != $card->admin_id ) && $acctuser->changeagent == 0)
			{
				return '充值卡所属代理不同' ; //代理不同
			}
			
			
			try {
				//充值日志
				Voucherlog::log($card, $acctuser);
				#没有设置unionacctname时 即开户
				if(!$acctuser->unionacctname && $unionuser && $unionuser<>$acctuser->acctname)$acctuser->unionacctname = $unionuser;
				$acctuser->balance = $acctuser->balance + $card->balance;
				$acctuser->voucherbalance += $card->balance;
				if($newAccUser == false)$acctuser->expireddays += $card->expireddays;
// 				if ($acctuser->expireddate == '0000-00-00')
// 				{
// 					$acctuser->expireddate = date('Y-m-d',strtotime("+ {$card->expireddays} day"));
// 				}else
// 				{
// 					$acctuser->expireddate = date('Y-m-d',strtotime("{$acctuser->expireddate} + {$card->expireddays} day"));
// 				}
			    if(strtotime($acctuser->expireddate)<time())
                {
                    $now = date('Y-m-d');
                    $acctuser->expireddate = date('Y-m-d',strtotime("{$now} + {$card->expireddays} day"));
                }else{
                    $acctuser->expireddate = date('Y-m-d',strtotime("{$acctuser->expireddate} + {$card->expireddays} day"));
                }
				$acctuser->save(0,'update');
				$card->vouchered = 1; //已经充值
				$card->enable = 0; //失效
				$card->activedate = date('Y-m-d');
				$card->voucher_acctname=$acctuser->acctname;
				$card->save(0,'update');
				#推广处理
				if ($acctuser->unionacctname && $acctuser->agent->unionset->id && !$card->payid)
				{
					$lv1 = $acctuser->agent->unionset->set_lv1;
					if(QValidator::validate_between($lv1, 0, 100))
					{
						#在0-100以内
						$sendmoney = floatval($card->balance * ($lv1/100));
						if($sendmoney>0){
							if (self::addmoney($acctuser->unionacctname, $sendmoney)){
								#加款成功
								Unionpaylog::addlog($acctuser->acctname, $acctuser->unionacctname, $sendmoney);
							}
						}
					}
				}
				#--------
				if($newAccUser && $monthFlag)
				{
					#包月卡流程
					Vouchercard::createBgTask($card, $acctuser);
				}
				
			} catch (Exception $e) {
				//dump($e->getMessage());
				return false;
			}
			//套餐
			if ($newAccUser && $card->packageid)
			{
				$packresult = Acctpackage::bind($acctuser->id, $card->packageid);
				Helper_Common::log(print_r($acctuser,true)."=>".print_r($card,true)."=>result:".$packresult,'package');
			}
			//
			if($newAccUser == false && $acctuser->changeagent == 1)
			{
				$acctuser->agent_id = $card->agent_id;
				$acctuser->admin_id = $card->admin_id;
				$acctuser->changeagent = 0;
				$acctuser->rategroupid = $card->rategroupid;
				$acctuser->rategroupid_acall = $card->rategroupid_acall;
				$acctuser->save(0);
			}
			try {
				//send msg
				if( ($acctuser->agent->smsurl  && $acctname && $card->balance > 0 && $acctuser->agent->smsflag && $acctuser->agent->adminsms) || $acctuser->agent->smstype == 2 )
				{
					if($acctuser->agent->smstype == 2)
					{
						$balance = self::formatBlance($acctuser->balance);
						$paybalance = self::formatBlance($card->balance);
						$outnewuser =array(
								'acct'=>Helper_Common::trim($acctuser->acctname),
								'caller'=>Helper_Common::trim($acctuser->acctname),
								'oparg'=>sprintf("%s-%s-%s",$paybalance,$acctuser->password,$balance),
								'created'=>date('Y-m-d H:i:s'),
								'type'=>1,
						);
						$obj = new outcall($outnewuser);
						$obj->save(0,'create');
						
					}else{
											$yu = $acctuser->balance ;
											if($acctuser->agent->smstype == 0)
												{
												    $defaultstr = "手机{$acctuser->acctname}本次充值金额为{$card->balance}，余额{$yu}密码{$acctuser->password}.";
												    $msg = $defaultstr.$acctuser->agent->smsmsg.$acctuser->agent->smssign;
													$msgmonye = 0;
												}else{
												    //$defaultstr = "({$acctuser->acctname})充值{$card->balance}元,余额{$yu},密码{$acctuser->password}。";
												    $msg = $acctuser->agent->smsmsg.$acctuser->agent->smssign;
													$msgmonye = 0.05;
												}
							
												$msg = str_replace('%balance%', $card->balance, $msg);
												$msg = str_replace('%exptime%', $card->expireddays, $msg);
												$msg = str_replace('%phone%', $acctuser->acctname, $msg);
												$msg = str_replace('%money%', $yu, $msg);
												$msg = str_replace('%password%', $acctuser->password, $msg);
												$msglength = mb_strlen($msg,'UTF-8');
												#$msg = iconv("UTF-8","GB2312",$msg);
												$msg = urlencode($msg);
												$url = str_replace('%m%', $acctname, $acctuser->agent->smsurl);
												$url = str_replace('%s%', $msg, $url);
							
												//$url = sprintf($acctuser->agent->smsurl,$acctname,$msg);
												//dump($url);die;
												$result = Helper_Common::cget($url);
												QLog::log('smssend:'.$url.'|'.$result);
												$result = iconv("GB2312","UTF-8",$result);
												QDB::getConn()->execute("call smscost({$acctuser->agent->id},$msgmonye)");
					}
				}
			
			} catch (Exception $e) {
				//return false;
			}
			return true;			
		}
	}
	static function transfer($acctname,$password,$tar_acctname,$tar_balance)
	{
		if($acctname == $tar_acctname )return '不能给自己转账';
		$acctuser = self::find('acctname = ? and password = ?',$acctname,$password)->getOne();
		if (!$acctuser->acctname){
			return '主帐号不存在';
		}else
		{
			if($tar_balance == -1)$tar_balance = $acctuser->balance; 
			#获取手续费绿
			$transferA = isset($acctuser->agent->unionset->id) ? floatval($acctuser->agent->unionset->transfer) : 0;
			if($transferA)
			{
				#存在费率 计算手续费
				$transferB = floatval(($tar_balance * $transferA) / 100);
			}else{
				$transferB = 0;
			}
			if($tar_balance > ($acctuser->balance+$transferB))
			{
				return '转账超过自身余额';#转账超过自身余额+手续费
			}
			
			$tar = self::find('acctname = ?',$tar_acctname)->getOne();
			if(!$tar->acctname){
				try {
					#不存在则创建心用户
					$tar = new self();
					$tar->agent_id = $acctuser->agent_id;
					$tar->admin_id = $acctuser->admin_id;
					
					$tar->acctname = $tar_acctname;
					$tar->password = substr($tar_acctname, -6);
					$tar->balance = $tar_balance;
					$tar->expireddate = $acctuser->expireddate;
					$tar->rategroupid = $acctuser->rategroupid;
					$tar->rategroupid_acall = $acctuser->rategroupid_acall;
					$tar->limitbalance = $acctuser->limitbalance;
					$tar->save(0,'create');
					$transferB += $tar_balance;
				} catch (Exception $e) {
				}
			}else
			{
				if($tar->rategroupid == $acctuser->rategroupid && $tar->rategroupid_acall == $acctuser->rategroupid_acall )
				{
					try {
						$tar->balance += $tar_balance;
						$tar->expireddate = date('Y-m-d',strtotime($tar->expireddate)+(strtotime($acctuser->expireddate)-time()));
						$tar->voucherbalance += $tar_balance;
						$tar->save(0,'update');
						//return true;
					} catch (Exception $e) {
						return '新开目标用户失败';
					}
				}else{
					#费率不同
					return '费率必须一样';
				}
				
			}
			if ($tar_balance == ($acctuser->balance + $transferB))
			{
				#全转 删除
				Acctuser::meta()->deleteWhere("id = {$acctuser->id}");
			}else{
				$acctuser->balance -= $tar_balance;
				$acctuser->costbalance += $tar_balance;
				$acctuser->save(0,'update');
			}
			
			return true;
		}
	}
	static function getUserBind($acctname)
	{
		$bindTel = Userbindtel::find('acctname = ? ',$acctname)->getAll();
		return $bindTel;
	}
	static function checkAcctname($acctname)
	{
		return self::find('acctname = ?',$acctname)->getOne();
	}
	static function addmoney($acctname,$addmoney)
	{
		$user = self::find('acctname = ?',$acctname)->getOne();
		if ($user->id())
		{
			$user->balance = $user->balance + floatval($addmoney);
			try {
				$user->save(0,'update');
				return true;
			} catch (Exception $e) {
				return false;
			}
		}else{
			return false;
		}
		
	}
	
	function bindnum()
	{
		$bind = usershortnum::find('acctname = ?',$this->acctname)->getOne();
		if ($bind->id())
		{
			return $bind->bindnum;
		}else {
			return '';
		}
		
	}
	function createDefaultweb800()
	{
		if (!$this->web800->id)
		{
			$obj = new Web800();
			$obj->acctuser_id = $this->id;
			$obj->agent_id = $this->agent_id;
			$obj->work_start = "00:00";
			$obj->work_end = "24:00";
			$obj->work_title = "";
			//$obj->created = date('Y-m-d H:i:s');
			$obj->save(0,'create');
		}
	}
	function user_attribStr()
	{
		$card = Vouchercard::find('cardname = ?',$this->vouchercard)->getOne();
		if($card->id())
		{
			return $card->cardtype == 1 ? sprintf('月卡(%.2f元)',$card->automoney):'普通';
		}else{
			return '普通';
		}
		
	}
	function user_vipstr()
	{
			$vip = Acctuservip::find('acct_id = ? and acctname = ?',$this->id,$this->acctname)->getOne();
			if ($vip->id())
			{
				return sprintf('VIP(%.2f元)',$vip->money);
			}else{
				return '';
			}
	}
	/**
	 * 插入认证码
	 */
	public static function insertAuth($acctname)
	{
			$cacheId = "orderAuth_".$acctname;
			$num = q::cache($cacheId,array('life_time'=>86400));
			
			if(!$num)
			{
				Q::writeCache($cacheId, 1);
			}else{
				Q::writeCache($cacheId,intval($num)+1);
			}
			if($num<=2)
			{
				QDB::getConn()->execute("INSERT into fscall.orderauth (acct,caller,authcode) VALUES('{$acctname}','{$acctname}',SUBSTRING(CONCAT(rand(now())),3,4))");
			}
		return $num;
	}
	public static function checkParentValibalance($agent_id)
	{
		$acct = Acct::find('id = ?',$agent_id)->getOne();
		if($acct->id() && $acct->balance > 0)
		{
			if(!$acct->is_admin)
			{
				return self::checkParentValibalance($acct->agent_id);
			}else{
				return true;
			}
		}else{
			return false;
		}
	}
	public static function get_acctname($id)
	{
		$user = self::find('id = ?',intval($id))->setColumns('acctname')->getOne();
		return $user->acctname;
		
	}
	public static function formatBlance($balance)
	{
		$result = '';
		$balance = sprintf("%.2f",$balance);
		if(($w = floor($balance/10000)) >0)$result.="{$w}w";
		$balance -= $w*10000;
		if(($q = floor($balance/1000)) >0)$result.="{$q}q";
		$balance -= $q*1000;
		//if($q == 0 && $w)$result.='0';
	
		if(($b = floor($balance/100)) >0)$result.="{$b}b";
	
		$balance -= $b*100;
		//if($b == 0 && $q)$result.='0';
	
		if(($s = floor($balance/10)) >0)$result.="{$s}s";
		$balance -= $s*10;
		//if($s == 0 && $b)$result.='0';
	
		if(($y = floor($balance/1)) >0 )$result.="{$y}";
	
		$balance -= $y*1;
	
		if($result)$result.='y';
		if(($j = floor($balance/0.1)) >0)$result.="{$j}j";
	
		$balance -= $j*0.1;
	
		if(($f = floor($balance/0.01)) >0)$result.="{$f}f";
		return $result;
	
	}
}

